<?php

class Quad_Route_HttpPretty extends Quad_Route_Http {
	
	public function __construct($url, $get) {
		$this->url = $url;
		$this->_get = $get;
    $parts = explode('/', $url);
    $this->_controllerName = $parts[0] ? array_shift($parts) : 'index';
    $this->_actionName = isset($parts[0]) ? array_shift($parts) : 'index'; 
    
    foreach ($parts as $i => $part) {
      if (($i % 2) == 0) {
        // even
        $field = rawurldecode($part);
        if (strlen($field)) {
          if (preg_match('/\[([\w_-]*)\]$/', $field, $match)) {
            if ($match[1]) {
              $akey = $match[1];
            }
            $this->_get[$field] = array();
            
          } elseif (isset($this->_get[$field])) {
            $this->_get[$field] = array($this->_get[$field]);
          }
        }
      } else {
        // odd
        $rawValue = rawurldecode($part);
        if ($field) {
          if (is_array($this->_get[$field])) {
            if ($akey) {
              $this->_get[$field][$akey] = $rawValue;
              $akey = null;
              
            } else {
              $this->_get[$field][] = $rawValue;
            }
          } else {
            $this->_get[$field] = $rawValue;
          }
        }
      }
    }
	}
	
  public function buildUrl($controller = 'index', $action = 'index', $params = array()) {
    $params['pg'] = "$controller.$action";
    if (count($params)) {
    	$get = '?' . http_build_query($params);
    } else {
    	$get = '';
    }
    return HTTP_ROOT . "/$controller/$action"; 
  }	
	
}